class Tag < ActiveRecord::Base
    include ModelHelper

    has_many :gists_tags
    has_many :gists, :through => :gists_tags

    validates_uniqueness_of :name
    validates_presence_of   :name

    def get_gists

    end

    ##########################################################
    # Use to generate a tag cloud
    # This method expects that tags contain a 'id', 'name' and a 'count' value
    def self.cloud(tags, category_list)

        if tags and tags.length > 0
            max = 0
            min = tags[0].count.to_i
            tags.each do |tag|
                count = tag.count.to_i
                max = count if count > max
                min = count if count < min
            end

            divisor = ((max - min) / category_list.size) + 1

            tags.each do |tag|
                yield tag, category_list[(tag.count.to_i - min) / divisor]
            end
        end
    end
    
end
